﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;

namespace B站直播开播提醒
{
    public partial class 主窗口 : Form
    {
        public static 主窗口 form1;

        public SoundPlayer sndPlayer_峰哥 = new SoundPlayer(Application.StartupPath + @"/ring_1.wav");
        public SoundPlayer sndPlayer_彤子 = new SoundPlayer(Application.StartupPath + @"/ring_2.wav");
        public SoundPlayer sndPlayer_宁子 = new SoundPlayer(Application.StartupPath + @"/ring_3.wav");
        public SoundPlayer sndPlayer_甲鱼 = new SoundPlayer(Application.StartupPath + @"/ring_4.wav");

        //public List<string> m_ListStr;
        public Dictionary<string, SoundPlayer> m_Dict;

        public 主窗口()
        {
            form1 = this;
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;

            m_Dict = new Dictionary<string, SoundPlayer>();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            // 窗体关闭之后
            notifyIcon1.Visible = false;//显示系统托盘图标
            Process.GetCurrentProcess().Kill();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 菜单按钮
            this.Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 窗体关闭之前

            //DialogResult result = MessageBox.Show("是否退出？选否，最小化到托盘", "操作提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            //if (result == DialogResult.Yes)
            //{
            //    this.Dispose();
            //    Application.Exit();
            //}
            //else
            //{
            //    e.Cancel = true;
            //    this.WindowState = FormWindowState.Minimized;
            //    this.Visible = false;
            //    this.notifyIcon1.Visible = true;
            //}
        }

        // 左键点击托盘图标
        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Show();
                this.Focus();
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                // 最小化
                this.ShowInTaskbar = false;     // 隐藏任务栏图标
                notifyIcon1.Visible = true;     // 显示系统托盘图标
                this.Hide();                    // 隐藏窗体
            }

        }

        // 开启查询
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;

            new Thread(() =>
            {
                Stopwatch stopwatch间隔时间 = Stopwatch.StartNew();
                while (true)
                {
                    if (stopwatch间隔时间.Elapsed.TotalSeconds >= 2)//2秒查询一次
                    {
                        stopwatch间隔时间.Restart();
                    }
                    else
                    {
                        Thread.Sleep(100);
                        continue;
                    }

                    foreach (var item in m_Dict)
                    {
                        CsharpHttpHelper.HttpItem httpItem2 = new CsharpHttpHelper.HttpItem()
                        {
                            Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                            ContentType = "application/json; charset=utf-8",
                            UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.190.400 QQBrowser/11.5.5240.400",
                            URL = "https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=" + item.Key + "&protocol=0,1&format=0,1,2&codec=0,1&qn=0&platform=web&ptype=8&dolby=5&panorama=1",
                            Method = "GET",
                        };

                        CsharpHttpHelper.HttpResult httpResult2 = new CsharpHttpHelper.HttpHelper().GetHtml(httpItem2);
                        if (httpResult2.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            if (httpResult2.Html.Contains("\"live_status\":1"))
                            {
                                //lb_state.Text = "正在直be中...";

                                // 弹出窗口
                                this.Show();
                                this.Focus();
                                this.WindowState = FormWindowState.Normal;

                                // 播放音乐
                                item.Value.PlayLooping();
                            }
                            else if (httpResult2.Html.Contains("\"live_status\":0"))
                            {
                                lb_state.Text = "正在监视ing 还没开播";
                            }
                            else
                            {
                                lb_state.Text = "查询失败";
                            }
                        }
                        else
                        {
                            lb_state.Text = "查询异常";
                        }
                    }

                }

            }).Start();
        }

        // 停止
        private void button2_Click(object sender, EventArgs e)
        {
            sndPlayer_峰哥.Stop();
        }

        // 双击 峰子textBox
        private void textBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //LiveRoomNum.Text = 峰子_LiveNum.Text;

            m_Dict.Add(峰子_LiveNum.Text, sndPlayer_峰哥);
            UpdateStr();
        }

        // 双击 彤子 textBox
        private void textBox3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //LiveRoomNum.Text = 彤子_LiveNum.Text;

            m_Dict.Add(彤子_LiveNum.Text, sndPlayer_彤子);
            UpdateStr();
        }

        // 双击 宁子 textBox
        private void textBox4_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //LiveRoomNum.Text = 宁子_LiveNum.Text;

            m_Dict.Add(宁子_LiveNum.Text, sndPlayer_宁子);
            UpdateStr();
        }

        // 双击 甲鱼 textBox
        private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            m_Dict.Add(甲鱼_LiveNum.Text, sndPlayer_甲鱼);
            UpdateStr();
        }

        public void UpdateStr()
        {
            string tmpStr = "";
            foreach (string strNum in m_Dict.Keys)
            {
                tmpStr += (strNum + ";");
            }

            LiveRoomNum.Text = tmpStr;
        }
    }
}
